/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.mapper;

import org.retabel.persistence.dao.document.ExportConfigurationDao;
import org.retabel.persistence.dao.report.ReportDesignPackageDao;
import org.retabel.persistence.entity.document.ExportConfiguration;
import org.retabel.persistence.entity.document.SectionConfiguration;
import org.retabel.persistence.entity.report.ReportDesign;
import org.retabel.persistence.entity.report.ReportDesignPackage;

import javax.ejb.EJB;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Class description.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 15:44 12.08.11
 *
 * @author Peter Herber
 * @version 1.0
 */
public abstract class XmlSectionConfigurationMapperBaseBean {

    /** Dao to access the designs. */
    @EJB
    protected ReportDesignPackageDao reportDesignPackageDao;

    /** Dao to access the export configurations. */
    @EJB
    protected ExportConfigurationDao exportConfigurationDao;

    /**
     * requires transaction
     *
     * @param sectConfiguration
     * @return
     * @throws XmlConfigurationMappingException
     */
    protected SectionConfiguration mapSectionConfiguration(xsd.rendersectionconfiguration.SectionConfiguration sectConfiguration)
        throws XmlConfigurationMappingException {
        SectionConfiguration sectionConfiguration = new SectionConfiguration();

        String designPackage = sectConfiguration.getDesignPackage();
        String designVersion = sectConfiguration.getDesignVersion();
        String designName = sectConfiguration.getDesignName();

        ReportDesignPackage reportDesignPackage = reportDesignPackageDao.findByNameAndVersion(designPackage, designVersion);

        if (reportDesignPackage == null) {
            throw new XmlConfigurationMappingException(
                "The report design package " + designPackage + " version " + designVersion + " does not exist!");
        }

        ReportDesign reportDesign = reportDesignPackage.getReportDesignByName(designName);

        if (reportDesign == null) {
            throw new XmlConfigurationMappingException(
                "The report design " + designPackage + " version " + designVersion + "design name " + designName
                + " does not exist!");
        }
        sectionConfiguration.setReportDesign(reportDesign);

        xsd.exportconfiguration.ExportConfiguration exportConfigurations = sectConfiguration.getExportConfigurations();
        List<xsd.exportconfiguration.ExportConfiguration.Export> exportList = exportConfigurations.getExport();
        Collection<ExportConfiguration> exportConfigurationCollection = new LinkedList<>();
        for (xsd.exportconfiguration.ExportConfiguration.Export export : exportList) {
            ExportConfiguration exportConfiguration = exportConfigurationDao.findByName(export.getRef());
            if (exportConfiguration == null) {
                throw new XmlConfigurationMappingException("The export configuration " + export.getRef() + " does not exist!");
            }
            exportConfigurationCollection.add(exportConfiguration);
        }
        sectionConfiguration.setExportConfigurations(exportConfigurationCollection);

        return sectionConfiguration;
    }

}
